package com.ripplezjw.blog.Service;

import com.ripplezjw.blog.mapper.ArtTagMapper;
import com.ripplezjw.blog.mapper.ArticleMapper;
import com.ripplezjw.blog.mapper.ClassificationMapper;
import com.ripplezjw.blog.mapper.TagMapper;
import com.ripplezjw.blog.po.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    ClassificationMapper classificationMapper;
    @Autowired
    ArtTagMapper artTagMapper;

    public String addArticle(Article article, String[] tags, String classification) {
        for (String tag : tags) {
            if (tagMapper.cntTagByTagName(tag) == 0) return "标签" + tag + "不存在";
        }

        if (classificationMapper.cntClassificationByClsName(classification) == 0) return "分类" + classification + "不存在";
        int clsId = classificationMapper.findClsIdByClsName(classification);
        article.setClsId(clsId);
        articleMapper.insertArticle(article);
        System.out.println("artId = " + article.getArticleId());
        for (String tag : tags) {
            int tagId = tagMapper.findTagIdByTagName(tag);
            artTagMapper.insertArtTagByArtIdAndTagId(article.getArticleId(), tagId);
        }
        return "";
    }

    public List<Article> showAllArticle() {
        return articleMapper.findArticle();
    }

    public void clickDelArticleBtn(int articleId) {
        artTagMapper.delArtTagByArticleId(articleId);
        articleMapper.delArticleByArticleId(articleId);
    }

    public Article clickModArticleBtn(int articleId) {
        return articleMapper.findArticleByArticleId(articleId);
    }

    public String modArticle(Article article, String[] tags, String classification) {
//        for (String tag : tags) {
//            if (tagMapper.cntTagByTagName(tag) == 0) return "标签" + tag + "不存在";
//        }

        if (classificationMapper.cntClassificationByClsName(classification) == 0) return "分类" + classification + "不存在";
        int clsId = classificationMapper.findClsIdByClsName(classification);
        article.setClsId(clsId);
        articleMapper.updateArticleByArticleId(article);
//        System.out.println("artId = " + article.getArticleId());
        for (String tag : tags) {
            System.out.println(tag);
            if (StringUtils.isEmpty(tag)) continue;
            if (tagMapper.cntTagByTagName(tag) == 0) continue;
            int tagId = tagMapper.findTagIdByTagName(tag);
            if (artTagMapper.cntTagIdAndArtId(tagId, article.getArticleId()) == 0)
                artTagMapper.insertArtTagByArtIdAndTagId(article.getArticleId(), tagId);
        }
        return "";
    }

    public Article getArticleById(int articleId) {
        return articleMapper.findArticleByArticleId(articleId);
    }

    public List<Article> findArticleByTime(String time) {
        return articleMapper.findArticleByTime(time);
    }

    public List<Article> findArticleByModTime(String time) {
        return articleMapper.findArticleByModTime(time);
    }

    public List<Date> getCreateTime() {
        return articleMapper.findCreateTime();
    }

    public List<Date> getModTime() {
        return articleMapper.findModTime();
    }
}
